<template>
  <div class="pagination-box">
    <el-pagination
      class="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.page"
      :page-sizes="PAGE_NUMBER"
      :page-size="page.pageSize"
      :layout="PAGINATION_LAYOUT"
      :total="total"
    />
  </div>
</template>

<script>
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common'
import G from 'lodash/get'
export default {
  name: 'Pagination',
  props: {
    requestFunc: {
      type: [Function],
      default: () => null
    },
    totalKey: {
      type: [String],
      default: 'payload.totalElements'
    },
    contentKey: {
      type: String,
      default: 'payload.content'
    }
  },
  data: () => {
    return {
      total: 0,
      PAGE_NUMBER,
      PAGINATION_LAYOUT,
      page: {
        page: 1,
        pageSize: PAGE_NUMBER[0]
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      this.page.page = 1
      this.page.pageSize = val
      this.getPageData()
    },
    handleCurrentChange (val) {
      this.page.page = val
      this.getPageData()
    },
    getPageData () {
      const total = this.totalKey
      const content = this.contentKey
      this.requestFunc()
        .then(res => {
          this.total = G(res, total, 0)
          this.$emit('returnData', G(res, content, []))
        })
        .catch(err => {
          console.log(err)
        })
    },
    Refresh () {
      this.page.page = 1
      this.page.pageSize = PAGE_NUMBER[0]
      this.getPageData()
    }
  },
  mounted: function () {
    this.getPageData()
  }
}
</script>

<style lang="less" scoped>
.pagination-box {
  .pagination {
    padding-top: 10px;
    display: flex;
    justify-content: flex-end;
    padding-right: 0;
  }
}
</style>
